from django.shortcuts import render, redirect, HttpResponse
from web.forms.project import ProjectAdd
from django.http import JsonResponse
from web import models


def project_list(request):
    """项目列表"""
    if request.method == 'GET':
        form = ProjectAdd(request)
        '''
        项目列表中有三部分： 1.星标项目 2.我创建的项目 3.我参与的项目
        上述三部分内容都来源于两个查询： 
        1.我创建的项目--星标、未星标
        2.我参与的项目--星标、未星标
        '''
        # 定义一个字典存储上述三部分内容，以减少查询操作
        project_list_dict = {'star': [], 'my': [], 'join': []}
        # 1.查询所有我创建的项目
        create_project_list = models.Project.objects.filter(creator=request.tracer.user)
        # 通过循环将星标与非星标的project对象存入字典中
        for row in create_project_list:
            if row.star:
                project_list_dict['star'].append({'value': row, 'type': 'my'})  # 星标时，追加一个字典到列表中，同时存储项目类型
            else:
                project_list_dict['my'].append(row)
        # 2.查询所有我参与的项目
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)  # 结果是ProjectUser对象的列表
        # 通过循环将星标与非星标项目的ProjectUser对象插入字典
        for obj in join_project_list:
            if obj.star:
                project_list_dict['star'].append({'value': obj.project, 'type':'join'})
            else:
                project_list_dict['join'].append(obj.project)
        context = {
            'form': form,
            'project_list_dict': project_list_dict,
        }
        return render(request, 'project_list.html', context)
    form = ProjectAdd(request, data=request.POST)
    if form.is_valid():
        # 因为项目表中还有创建人字段是不允许为空的且并不在前端表单上，所以保存前需要增加该字段
        form.instance.creator = request.tracer.user
        form.save()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'errors': form.errors})


def project_star(request, project_type, project_id):
    """项目星标操作"""
    # 1.我创建的项目星标操作
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')
    # 2.我参与项目星标操作
    if project_type == 'join':
        models.ProjectUser.objects.filter(project=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')
    return HttpResponse('请求参数不合法!')


"""
写法1 通过创建者和当前操作者判断
def project_unstar(request, project_creator, project_id):
    # 星标项目取消星标
    # 1.如果项目创建者是当前用户
    if int(project_creator) == request.tracer.user.id:
        models.Project.objects.filter(creator=request.tracer.user, id=project_id).update(star=False)
        return redirect('project_list')
    # 2.如果项目创建者不是当前用户
    else:
        models.ProjectUser.objects.filter(user=request.tracer.user, project=project_id).update(star=False)
        return redirect('project_list')
"""

def project_unstar(request, project_type, project_id):
    """取消星标操作"""
    # 1.我创建的项目星标操作
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')
    # 2.我参与项目星标操作
    if project_type == 'join':
        models.ProjectUser.objects.filter(project=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')
    return HttpResponse('请求参数不合法!')
